package com.applink.sell_proof.adapter;

import java.util.concurrent.ConcurrentHashMap;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.R;
import com.applink.sell_proof.builder.AbstractBuilder;

public class ListBaseAdapter extends BaseAdapter {

	protected AbstractBuilder	mBuilder;
	private int					mLimitNumber	= -1;

	public ListBaseAdapter(AbstractBuilder builder) {
		mBuilder = builder;
	}

	@Override
	public int getCount() {
		return mLimitNumber < 0 ? mBuilder.getData().size() : Math.min(mLimitNumber, mBuilder.getData().size());
	}

	@Override
	public Object getItem(int position) {
		return mBuilder.getData().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		// this function purpose is to generate the view layout on each item.
		// The trick is that try to create static reference to Views objects so
		// with 1000 items Views still would be the same amount of memory, just
		// only the data size will be increased/updated.

		synchronized (mBuilder.getData()) {
			if (mBuilder.getData() == null || mBuilder.getData().size() == 0) {
				return null;
			}

			ViewsReference viewref;

			// check if it's could reuse the old view or not
			if (convertView == null) {

				// create a new instance of the Views static references
				viewref = new ViewsReference();

				// initialize the convertView
				convertView = mBuilder.buildConvertView();

				// generate all the child views and stored it to static views
				// reference
				viewref.views = mBuilder.buildConvertViewContent(convertView);

				// attach views ref as Tag to convertView
				convertView.setTag(viewref);

			} else {
				// the old view could be reuse. Load the reference layouts
				viewref = (ViewsReference) convertView.getTag();
			}

			// build data which associate with an item on list
			mBuilder.builConvertViewContentData(position, viewref.views, mBuilder.getData());

			return convertView;
		}
	}

	/**
	 * 
	 * set a limitation for how many items could show in the list and at the same time update the all related views
	 * 
	 * @param limiter
	 */
	public void setItemsLimit(int limiter) {

		// if there is a change in quantities
		if (limiter != mLimitNumber) {

			mLimitNumber = limiter;

			// update all the views by notify data was changed
			notifyDataSetChanged();
		}
	}

	public boolean showMoreAble() {
		return (mBuilder.getData().size() > mLimitNumber) && (mLimitNumber > 0);
	}

	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		return LayoutInflater.from(EMarketing.getInstance().getApplicationContext()).inflate(R.layout.tier_item, null);
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		return position % 2;
	}

	/**
	 * 
	 * static object which content reference to Views list in order to make sure that view layout object could be reused
	 * 
	 * @author Van Vu
	 * 
	 */
	private static class ViewsReference {
		ConcurrentHashMap<Integer, View>	views;
	}

}
